<template>
  <div class="prescriptions-card card">
    <div class="card-header">
      <i class="fas fa-file-prescription card-icon"></i>
      <span class="card-title">电子处方处理</span>
      <span class="badge-count">{{ filteredPrescriptions.length }}</span>
    </div>
    <div class="card-content">
      <div class="filter-tabs">
        <button
          v-for="tab in filterTabs"
          :key="tab.value"
          class="filter-tab"
          :class="{ active: filter === tab.value }"
          @click="handleFilterChange(tab.value)"
        >
          {{ tab.label }}
        </button>
      </div>

      <div class="prescriptions-container">
        <div
          v-for="prescription in filteredPrescriptions"
          :key="prescription.id"
          class="prescription-card"
          :class="prescription.status"
          @click="selectPrescription(prescription)"
        >
          <div class="prescription-header">
            <div class="prescription-meta">
              <span class="prescription-id">{{ prescription.id }}</span>
              <span class="prescription-time">{{ formatTime(prescription.createTime) }}</span>
            </div>
            <div class="status-badge" :class="prescription.status">
              {{ getStatusText(prescription.status) }}
            </div>
          </div>
          <div class="patient-info">
            <div class="patient-avatar">
              {{ prescription.patientGender === '男' ? '👨' : '👩' }}
            </div>
            <div class="patient-details">
              <h4 class="patient-name">{{ prescription.patientName }}</h4>
              <span class="patient-demographics">{{ prescription.patientAge }}岁 {{ prescription.department }}</span>
            </div>
          </div>
          <div class="prescription-footer">
            <div class="doctor-info">
              <i class="fas fa-user-md"></i>
              {{ prescription.doctorName }}
            </div>
            <div class="medicine-count">
              {{ prescription.medicines.length }} 种药品
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

// 定义props
const props = defineProps({
  prescriptions: {
    type: Array,
    required: true
  },
  filter: {
    type: String,
    default: 'all'
  }
})

// 定义事件
const emit = defineEmits(['filter-change', 'select-prescription'])

// 筛选标签
const filterTabs = [
  { label: '全部', value: 'all' },
  { label: '待处理', value: 'pending' },
  { label: '处理中', value: 'processing' },
  { label: '已完成', value: 'completed' }
]

// 计算属性
const filteredPrescriptions = computed(() => {
  if (props.filter === 'all') return props.prescriptions
  return props.prescriptions.filter(p => p.status === props.filter)
})

// 方法
const handleFilterChange = (value) => {
  emit('filter-change', value)
}

const selectPrescription = (prescription) => {
  emit('select-prescription', prescription)
}

const getStatusText = (status) => {
  const statusMap = {
    'pending': '待处理',
    'processing': '处理中',
    'completed': '已完成'
  }
  return statusMap[status] || status
}

const formatTime = (timeString) => {
  if (!timeString) return ''
  const date = new Date(timeString)
  return date.toLocaleString('zh-CN')
}
</script>

<style scoped>
.card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
  margin-bottom: 20px;
}

.card:hover {
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  transform: translateY(-2px);
}

.card-header {
  display: flex;
  align-items: center;
  padding: 20px 24px 16px;
  border-bottom: 1px solid #f0f2f5;
}

.card-icon {
  font-size: 20px;
  color: #4a6cf7;
  margin-right: 10px;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
  flex: 1;
}

.badge-count {
  background: #4a6cf7;
  color: white;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.card-content {
  padding: 20px 24px;
}

.filter-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
  padding: 4px;
  background: #f8f9fc;
  border-radius: 10px;
}

.filter-tab {
  flex: 1;
  padding: 8px 12px;
  border: none;
  background: transparent;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
}

.filter-tab.active {
  background: white;
  color: #4a6cf7;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.prescriptions-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 500px;
  overflow-y: auto;
}

.prescription-card {
  padding: 16px;
  border: 2px solid #e8eaf0;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s;
  background: white;
}

.prescription-card:hover {
  border-color: #4a6cf7;
  transform: translateY(-2px);
}

.prescription-card.pending {
  border-left: 4px solid #ff6b6b;
}

.prescription-card.processing {
  border-left: 4px solid #ffd166;
}

.prescription-card.completed {
  border-left: 4px solid #0ca678;
}

.prescription-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
}

.prescription-meta {
  display: flex;
  flex-direction: column;
}

.prescription-id {
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 4px;
}

.prescription-time {
  font-size: 12px;
  color: #8f9bb3;
}

.status-badge {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}

.status-badge.pending {
  background: #ffe3e3;
  color: #fa5252;
}

.status-badge.processing {
  background: #fff9e6;
  color: #f59f00;
}

.status-badge.completed {
  background: #e7f7ef;
  color: #0ca678;
}

.patient-info {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.patient-avatar {
  font-size: 32px;
}

.patient-details {
  flex: 1;
}

.patient-name {
  font-weight: 600;
  color: #1a1a1a;
  margin: 0 0 4px 0;
}

.patient-demographics {
  font-size: 14px;
  color: #8f9bb3;
}

.prescription-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.doctor-info {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: #8f9bb3;
}

.medicine-count {
  font-size: 14px;
  color: #8f9bb3;
}
</style>